import { ref } from 'vue'
import { getFriendList, userId, userName, user } from './user'
import { groupUserList } from './user'
import axios from 'axios'
import { ElMessage, tourProps } from 'element-plus'
import { friendList } from './getFriend'
import  router  from '../router'


// 页面切换拷贝
const middlePageCopy = ref(0)
const rightPageCopy = ref(0)
const mainPageCopy = ref(1)

// 页面切换
export const middlePage = ref(1)
export const rightPage = ref(0)
export const mainPage = ref(1)
export const right_bottomPage = ref(1)
//判断创建群聊弹窗
export const dialogVisible = ref(false)
//判断是好友还是群聊
export const type = ref(0)
export const groupName = ref('')
const groupNameCopy = ref('')
const groupUserListCopy = ref([])

//要添加的信息
export const addInfo = ref()
//判断个人信息弹窗
export const perDialogVisible = ref(false)

// 切换消息页面
export function message(){
  middlePage.value = 1
  rightPage.value = 0
  mainPage.value = 1
}

//切换添加好友页面
export function add(){
  middlePage.value = 2
  rightPage.value = 0
  mainPage.value = 1
}

//切换创建群聊页面
export function createGroup(){
    middlePageCopy.value = middlePage.value
    rightPageCopy.value = rightPage.value
    mainPageCopy.value = mainPage.value
    mainPage.value = 0
    middlePage.value = 0
    rightPage.value = 0
    dialogVisible.value = true
    getFriendList()
}

//重置
export function revert(){
    dialogVisible.value = false
    middlePage.value = middlePageCopy.value
    rightPage.value = rightPageCopy.value
    mainPage.value = mainPageCopy.value
    groupUserListCopy.value = groupUserList.value
    groupUserList.value = [{"id" : userId.value, "name" : userName.value}]
    type.value = 0
    groupNameCopy.value = groupName.value
    groupName.value = ''
}

//创建群聊
export function create(){
    const groupUserIds = ref([])
    for(let i = 0; i < groupUserListCopy.value.length; i++){
        groupUserIds.value.push(groupUserListCopy.value[i].id)
    }
    axios({
        method : 'post',
        url : 'https://47.120.69.214:8787/group/add',
        data : {
            "masterId" : userId.value,
            "name" : groupNameCopy.value,
            "userIdList" : groupUserIds.value
        },
        headers : {
            'Content-Type' : 'application/json'
        }
    }).then((response) => {
        // console.log(response.data)
        friendList.value.unshift(response.data)
        console.log(friendList.value)
        if (response.status === 200) return Promise.resolve(ElMessage.success('群聊创建成功'))
        
    }).catch((error) => {
    console.log(error)
})
}

//判断所要添加的用户是否已经是好友
export const isFriend = ref()
export function judgeIsFriend(){
    if(addInfo.value.id === userId.value){
        isFriend.value = true
        return ;
    }
    axios({
        method : 'get',
        url : 'https://47.120.69.214:8787/friend/getAllFriendOf',
        params : {
            "userId" : userId.value
        }
    }).then(response => {
        isFriend.value = false
        for(let i = 0; i < response.data.length; i++){
            const item = response.data[i]
            if(item.id === addInfo.value.id){
                isFriend.value = true
                return;
            }
        }
    }).catch(error => console.log(error))
}

//判断所要添加的群聊是否已经加入
export const isAddGroup = ref()
export function judgeIsAddGroup(){
    axios({
        menthod : 'get',
        url : 'https://47.120.69.214:8787/group/getAllJoinedGroup',
        params : {
            "userId" : userId.value
        }
    }).then( response => {
        isAddGroup.value = false
        for(let i = 0; i < response.data.length; i++){
            const item = response.data[i]
            if(item.id === addInfo.value.id){
                isAddGroup.value = true 
                return;
            }}
    })
    .catch(error => console.log(error))
}

//展示个人信息
export function showPersonalInfo(){
    perDialogVisible.value = true
    middlePageCopy.value = middlePage.value
    rightPageCopy.value = rightPage.value
    mainPageCopy.value = mainPage.value
    middlePage.value = 0
    rightPage.value = 0
    mainPage.value = 0
}

//退出个人信息界面
export function exitPersonalInfo(){
    middlePage.value = middlePageCopy.value
    rightPage.value = rightPageCopy.value
    mainPage.value = mainPageCopy.value
}

//好友和群聊申请页面
export function apply(){
    middlePageCopy.value = middlePage.value
    rightPageCopy.value = rightPage.value
    middlePage.value = 0
    rightPage.value = 0
    mainPage.value = 2
}


//聊天框预览图片
export function openImage(){
    // console.log('openImage')
    right_bottomPage.value = 0
}

//关闭预览图片
export function closeImage(){
    // console.log('closeImage')
    right_bottomPage.value = 1
}

//退出登录
export function exit(){
    router.push('/loginInterface')
}


//朋友圈
export function circleOfFriends(){
    middlePage.value = 0
    rightPage.value = 0
    mainPage.value = 3
    // router.push('/circleOfFriends')
}

//好友和群聊的信息
export const friendAndGroupInfoDrawer = ref(false)

